package com.dushuren.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dushuren.project.common.ErrorCode;
import com.dushuren.project.exception.BusinessException;
import com.dushuren.project.mapper.TeamCureExamineMapper;
import com.dushuren.project.model.dto.TeamUpdateCureExamineRequest;
import com.dushuren.project.model.entity.TeamCureExamine;
import com.dushuren.project.service.TeamCureExamineService;
import com.dushuren.project.utils.DatesUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.dushuren.project.constant.TeamConstant.TEAM_DESCRIPTION_MAX;
import static com.dushuren.project.constant.TeamCureExamineConstant.*;

/**
* @author qieqie
* @description 针对表【team_cure_examine(队伍公开信息审核表)】的数据库操作Service实现
* @createDate 2022-10-17 17:53:09
*/
@Service
public class TeamCureExamineServiceImpl extends ServiceImpl<TeamCureExamineMapper, TeamCureExamine>
    implements TeamCureExamineService {

  @Resource
  TeamCureExamineMapper teamCureExamineMapper;

  @Override
  public Boolean checkUpdateDate(Long teamId, String teamName) {
	// 校验 本月修改次数
	QueryWrapper<TeamCureExamine> queryWrapper = new QueryWrapper<>();
	queryWrapper.eq("teamCureCategory", teamName);
	queryWrapper.eq("teamId", teamId);
	queryWrapper.apply("UNIX_TIMESTAMP(createTime) <= UNIX_TIMESTAMP('" + DatesUtils.getLastDay() + "')");
	queryWrapper.apply("UNIX_TIMESTAMP(createTime) > UNIX_TIMESTAMP('" + DatesUtils.getFirstDay() + "')");
	Long count = teamCureExamineMapper.selectCount(queryWrapper);
	if (count+1 > 3 ) {
	  return false;
	}
	return true;
  }

  @Override
  public Boolean checkUpdateApply(Long teamId, String teamName) {
	// 校验 是否存在为审批的 申请
	QueryWrapper<TeamCureExamine> queryWrapperTeam = new QueryWrapper<>();
	queryWrapperTeam.eq("teamCureCategory", teamName);
	queryWrapperTeam.eq("teamId", teamId);
	queryWrapperTeam.eq("examineState", 0);
	Long count1 = teamCureExamineMapper.selectCount(queryWrapperTeam);
	if (count1 != 0) {
		return false;
	}
	return true;
  }

  /***
   * 增加 申请队伍信息
   */
  @Override
  public Long addApply(Long userId , Long teamId, String type, String description) {
	Boolean examineState = this.checkUpdateApply(teamId,type);
	// todo 暂时没有启动 修改限制次数
//	Boolean resultTeamName = this.checkUpdateDate(teamId,type);
	if (Boolean.TRUE.equals(!examineState)) {
	  throw new BusinessException(ErrorCode.PARAMS_ERROR,"等待之前的审核");
	}
	TeamCureExamine teamCureExamine = new TeamCureExamine();
	teamCureExamine.setUserId(userId);
	teamCureExamine.setTeamId(teamId);
	teamCureExamine.setTeamCureCategory(type);
	teamCureExamine.setTeamInformation(description);
	this.save(teamCureExamine);
	return 0L;
  }

  /***
   * 队伍公开消息审核 分页查询
   * @param updateCureExamineRequest
   * @param examineState
   * @return
   */
  @Override
  public Page<TeamCureExamine> listQueryTeam(TeamUpdateCureExamineRequest updateCureExamineRequest, Integer examineState) {
	Long id = updateCureExamineRequest.getId();
	Long userId = updateCureExamineRequest.getUserId();
	Long teamId = updateCureExamineRequest.getTeamId();
	String teamCureCategory = updateCureExamineRequest.getTeamCureCategory();
	String teamInformation = updateCureExamineRequest.getTeamInformation();

	QueryWrapper<TeamCureExamine> queryWrapperTeam = new QueryWrapper<>();
	queryWrapperTeam.select("id", "userId", "teamId",
	  "teamCureCategory", "teamInformation","createTime", "examineState", "examineInformation");
	queryWrapperTeam.eq("examineState" ,examineState);
	if (id != null && id > 0) {
	  queryWrapperTeam.eq("id", id);
	}
	if (userId != null && userId > 0) {
	  queryWrapperTeam.eq("userId",userId);
	}
	if (teamId != null && teamId > 0) {
	  queryWrapperTeam.eq("teamId", teamId);
	}
	if (teamCureCategory != null && (teamCureCategory.equals(TEAM_CURE_EXAMINE_NAME) || teamCureCategory.equals(TEAM_CURE_EXAMINE_DESCRIPTION) || teamCureCategory.equals(TEAM_CURE_EXAMINE_AvatarUrl))) {
	  queryWrapperTeam.eq("teamCureCategory", teamCureCategory);
	}
	if (teamInformation != null && teamInformation.length() <= TEAM_DESCRIPTION_MAX) {
	  queryWrapperTeam.like("teamInformation", teamInformation);
	}
	long pageSize = updateCureExamineRequest.getPageSize();
	long current = updateCureExamineRequest.getCurrent();
	Page<TeamCureExamine> page = this.page(new Page<>(current, pageSize), queryWrapperTeam);
	return page;
  }

  /**
   * 校验TeamCureExamine表中这个Id是否存在，teamApplyState 是否为 0 》 是否为审核状态
   */
  @Override
  public TeamCureExamine checkApplyState(Long id) {
	if (id <= 0) {
	  throw new BusinessException(ErrorCode.NULL_ERROR);
	}
	QueryWrapper<TeamCureExamine> queryWrapper = new QueryWrapper<>();
	queryWrapper.select("id", "userId" , "teamId", "teamCureCategory", "teamInformation" , "examineState");
	queryWrapper.eq("id", id);
	queryWrapper.eq("examineState ", 0);
	TeamCureExamine teamCureExamine = teamCureExamineMapper.selectOne(queryWrapper);
	if (teamCureExamine == null) {
	  throw new BusinessException(ErrorCode.PARAMS_ERROR);
	}
	return teamCureExamine;
  }

  @Override
  public Long cureApplyTeam(Long teamId) {
	if (teamId <= 0) {
	  throw new BusinessException(ErrorCode.NULL_ERROR);
	}
	QueryWrapper<TeamCureExamine> queryWrapper = new QueryWrapper<>();
	queryWrapper.select("id");
	queryWrapper.eq("teamId", teamId);
	queryWrapper.eq("examineState ", 0); // 0 待审核 1 通过 2 拒绝
	Long count = teamCureExamineMapper.selectCount(queryWrapper);
	if (count == 0) {
	  return 0L;
	}
	return 1L;
  }
}




